﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Common;
using MarginTrading.Services;

namespace MarginTrading.Forms
{
    public partial class AddAgent : Form
    {
        public AddAgent()
        {
            InitializeComponent();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (validateInputs() == false)
            {
                return;
            }

            Agent agent = new Agent();
            agent.Code = textBoxAgentCode.Text;
            agent.Name = textBoxAgentName.Text;
            agent.Address_1 = textBoxAddress1.Text;
            agent.Address_2 = textBoxAddress2.Text;
            agent.Telephone1 = textBoxTelephone.Text;
            agent.Fax = textBoxFax.Text;
            agent.CreatedDate = DateTime.Today;
            agent.Interest_Rate = double.Parse(textBoxInterestRate.Text);

            try
            {
                AgentServices.Add(agent);
            }
            catch (System.Exception ex)
            {
                if (ex.Message.StartsWith("ORA-00001: unique"))
                {
                    MessageBox.Show("Agent " + textBoxAgentCode.Text + " already exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("Failed to create new agent\nError: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                return;
            }
            MessageBox.Show("Agent added successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            
            if (checkBoxDontClose.Checked == false)
            {
                Close();
            }
            else
            {
                textBoxAgentCode.Text = "";
                textBoxAgentName.Text = "";
                textBoxAddress1.Text = "";
                textBoxAddress2.Text = "";
                textBoxTelephone.Text = "";
                textBoxFax.Text = "";
                textBoxInterestRate.Text = "";

                textBoxAgentCode.Focus();
            }
        }

        private bool validateInputs()
        {
            if (textBoxAgentCode.Text == "")
            {
                MessageBox.Show("Enter agent code", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            double fMarginLimit = 0;
            if (double.TryParse(textBoxInterestRate.Text, out fMarginLimit) == false)
            {
                MessageBox.Show("Invalid Interest Rate", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            return true;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void AddAgent_Load(object sender, EventArgs e)
        {

        }
    }
}
